<script setup lang="ts">
import { ref } from 'vue'
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import { showToast } from 'vant'
import { loginByPassword, loginByMobile } from '@/services/user'
import { useUserStore } from '@/stores'
import { useRoute, useRouter } from 'vue-router'
import { useSendMobileCode } from '@/composable'
import axios from 'axios'

// 测试mock接口
axios.get('/patient/message/sys/list').then((res) => {
  console.log(res)
})

axios.post('/cc/bb/dd').then((res) => {
  console.log(res)
})

const store = useUserStore()
const route = useRoute()
const router = useRouter()

// 1. 密码登录
const mobile = ref('')
const password = ref('')
const agree = ref(false)
const login = async () => {
  // 1.1 判断用户是否勾选同意协议
  if (!agree.value) return showToast('请勾选同意用户协议')
  // 1.2 调用接口函数
  const res = isPass.value
    ? await loginByPassword(mobile.value, password.value)
    : await loginByMobile(mobile.value, code.value)
  // 1.3 存储用户信息
  store.setUser(res.data)
  // 1.4 回跳(有回跳页面则去回跳页面, 没有去个人中心)
  router.replace((route.query.returnUrl as string) || '/user')
  // 1.5 提示用户
  showToast('登录成功~')
}

// 2. 验证码登录
const isPass = ref(true)
const code = ref('')
const { time, form, sendCode } = useSendMobileCode(mobile, 'login')

// 3. 明文或密文
const show = ref(false) // false: 密文  / true: 明文

// https://graph.qq.com/oauth2.0/authorize?client_id=102015968&response_type=token&scope=all&redirect_uri=http://consult-patients.itheima.net/login/callback
const qqUrl = `https://graph.qq.com/oauth2.0/authorize?client_id=102015968&response_type=token&scope=all&redirect_uri=${encodeURIComponent(
  import.meta.env.VITE_APP_CALLBACK + '/login/callback'
)}`
// console.log(qqUrl)
</script>

<template>
  <div class="login-page">
    <cp-nav-bar
      right-text="注册"
      @click-right="$router.push('/register')"
    ></cp-nav-bar>
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
      <a href="javascript:;" @click="isPass = !isPass">
        <span>{{ isPass ? '短信验证码登录' : '密码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form ref="form" autocomplete="off" @submit="login">
      <van-field
        name="mobile"
        v-model="mobile"
        placeholder="请输入手机号"
        type="tel"
        :rules="mobileRules"
      ></van-field>
      <!-- 判断密码登录还是验证码登录 -->
      <van-field
        v-if="isPass"
        v-model="password"
        placeholder="请输入密码"
        :type="`${show ? 'text' : 'password'}`"
        :rules="passwordRules"
      >
        <template #right-icon>
          <cp-icon
            @click="show = !show"
            :name="`login-eye-${!show ? 'off' : 'on'}`"
          />
        </template>
      </van-field>
      <van-field
        v-else
        v-model="code"
        placeholder="请输入短信验证码"
        :rules="codeRules"
      >
        <template #button>
          <span
            class="btn-send"
            :class="{ active: time > 0 }"
            @click="sendCode"
          >
            {{ time > 0 ? `${time}s后再次发送` : '发送验证码' }}
          </span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit"
          >登 录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <!-- QQ登录 -->
      <a
        class="icon"
        :href="qqUrl"
        @click="store.setReturnUrl(route.query.returnUrl as string)"
      >
        <img src="@/assets/qq.svg" alt="" />
      </a>
      <!-- <span id="qqLoginBtn"></span> -->
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import '@/styles/login.scss';
</style>
